package com.iflytek.uoamp.agent.monitor.lib;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.apache.log4j.Logger;

import com.iflytek.uoamp.agent.core.sender.monitorsender.requestObj.MonitorItemConfig;
import com.iflytek.uoamp.agent.core.sender.monitorsender.requestObj.MonitorItemResult;
import com.iflytek.uoamp.agent.core.sender.monitorsender.responseObj.ThriftFlumeEvent;
import com.iflytek.uoamp.agent.monitor.find.FinderThread;
import com.iflytek.uoamp.agent.monitor.lib.statics.Statics;
import com.iflytek.uoamp.agent.monitor.mcagent.MCAgentLog;
import com.iflytek.uoamp.agent.monitor.mcagent.MCAgentMain;
import com.iflytek.uoamp.agent.monitor.mcagent.MonitorResultSender;

/**
 * @author gaowei 处理 lib 发送过来的数据
 */
public class ParseEventList implements Runnable {

	private static List<ThriftFlumeEvent> list = Collections.synchronizedList(new LinkedList<ThriftFlumeEvent>());
	private static ParseLibResult parser = new ParseLibResult();

	private static Statics staticser = new Statics();

	private void sendInfo(MonitorItemResult libResult) {
		try {
			List<MonitorItemResult> linked = new LinkedList<MonitorItemResult>();
			linked.add(libResult);
			List<MonitorItemConfig> uplist = MonitorResultSender.sendMonitorInfo(linked, ParseLibResult.setToString(), "checklib");
			parser.init(uplist);

		} catch (Exception e) {
			Logger.getRootLogger().error("send lib info error!!", e);
		}
	}

	public static ParseLibResult getParser() {
		return parser;
	}

	public static void setParser(ParseLibResult parser) {
		ParseEventList.parser = parser;
	}

	public void parse() {
		while (true) {
			try {
				ThriftFlumeEvent event = null;
				try {
					if(!list.isEmpty()){
						event = list.remove(0);
					}
				} catch (Exception e) {
					Logger.getRootLogger().error(e.getMessage(),e);
				}

				if (event == null) {
					try {
						Thread.sleep(5);
					} catch (InterruptedException e) {
					}
					continue;
				}
				String isMonitor = event.getHeaders().get("sendtype");
				String logtype = event.getHeaders().get("logtype");
				String collectionName = event.getHeaders().get("collectionName");
				if (collectionName != null && collectionName.toLowerCase().equals("startlog")) {
					MCAgentMain.getSenderClient().send(new MCAgentLog(event.exchangeBodyToByte()));
				} else if (isMonitor == null || !isMonitor.equals("sendMonitor")) {
					Logger.getRootLogger().debug("get item " + new String(event.exchangeBodyToByte()));
					MCAgentMain.getSenderClient().send(new LibSendObj(event.getHeaders(), event.getBody()));
				} else if (logtype != null && logtype.toLowerCase().equals("thirdparty")) {
					String cpname = event.getHeaders().get("cpname");
					String localip = event.getHeaders().get("localip");
					String raqadd = event.getHeaders().get("raqadd");
					staticser.add(cpname + (char) 31 + localip + (char) 31 + raqadd);
				} else {

					// 自动发现 需要添加
					String ip = event.getHeaders().get("sendIP");
					String sendComponent = event.getHeaders().get("sendComponent");
					Logger.getRootLogger().info("get " + ip + "  " + sendComponent + " send static info " + new String(event.exchangeBodyToByte()));
					MonitorItemResult libResult = parser.getErrorLevel(ip, sendComponent, event.exchangeBodyToByte());
					// add lib finder
					FinderThread.getFindLib().add(ip, sendComponent);
					// send monitor info for nagios
					sendInfo(libResult);
					// send lib history
					MCAgentMain.getSenderClient().send(new LibSendObj(event.getHeaders(), event.getBody()));
				}
			} catch (Exception e) {
				Logger.getRootLogger().error(e.getMessage(), e);
			}
		}
	}

	public static void add(ThriftFlumeEvent cur) {
		list.add(cur);
	}

	public static void addAll(List<ThriftFlumeEvent> events) {
		list.addAll(events);
	}

	@Override
	public void run() {
		parse();
	}

}
